﻿using System;
using System.ServiceProcess;
using System.Windows.Forms;
using Setup.Interfaces;

namespace Setup
{
    public partial class Form1 : Form
    {
        public const int WelcomeTab = 0;
        public int CurrentTabIndex = WelcomeTab;

        public Form1()
        {
            InitializeComponent();

            tabControl1.SelectedIndex = WelcomeTab;

            tabControl1.Selecting += tabControl1_Selecting;
            tabControl1.Selected += tabControl1_Selected;
        }

        void tabControl1_Selected(object sender, TabControlEventArgs e)
        {
            if (e.Action == TabControlAction.Selected)
            {
                var controls = tabControl1.TabPages[CurrentTabIndex].Controls;
                if (controls.Count > 0)
                {
                    foreach (var control in controls)
                    {
                        var tabControl = control as ITabControl;
                        if (tabControl != null)
                        {
                            if (!tabControl.IsComplete)
                            {
                                tabControl.Start();
                            }
                        }
                    }
                }

            }
        }

        void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
        {
            var controls = tabControl1.TabPages[CurrentTabIndex].Controls;
            if (controls.Count > 0)
            {
                foreach (var control in controls)
                {
                    var tabControl = control as ITabControl;
                    if (tabControl != null)
                    {
                        var isNextTab = e.TabPageIndex > CurrentTabIndex;
                        if (!tabControl.IsComplete && isNextTab)
                        {
                            MessageBox.Show("Current tab has to be completed before you can continue.");
                            e.Cancel = true;
                            break;
                        }
                        else
                        {
                            tabControl.Stop();
                        }
                    }
                }
            }

            if (!e.Cancel)
            {
                CurrentTabIndex = e.TabPageIndex;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void storage1_Load(object sender, EventArgs e)
        {
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            var nextIndex = tabControl1.SelectedIndex + 1;
            if (nextIndex < tabControl1.TabCount)
            {
                tabControl1.SelectTab(nextIndex);
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            var hasRavenDbAsService = false;
            var hasFlowMediaAsService = false;

            var services = ServiceController.GetServices();
            foreach (var service in services)
            {
                var isRavenDb = "RavenDB".Equals(service.ServiceName, StringComparison.CurrentCultureIgnoreCase);
                var isFlowMedia = "FlowMedia".Equals(service.ServiceName, StringComparison.CurrentCultureIgnoreCase);
                if (isRavenDb)
                {
                    hasRavenDbAsService = true;
                }
                else if (isFlowMedia)
                {
                    hasFlowMediaAsService = true;
                }

                if (hasFlowMediaAsService && hasRavenDbAsService)
                    break;
            }

            if (!hasRavenDbAsService)
            {
                // TODO: Install RavenDB as service.
            }

            if (!hasFlowMediaAsService)
            {
                // TODO: Install FlowMedia as Service
            }

            if (hasFlowMediaAsService && hasRavenDbAsService)
            {
                MessageBox.Show("Congratulation. You have now installed FlowMedia");    

            }
        }
    }
}
